﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Net.Json;

namespace AFT2
{
    [Serializable]
    public class ApplicationLoader
    {
        [Serializable]
        public class KaKaoTalk
        {
            [Serializable]
            public class Friends
            {
                public long type { get; set; }
                public string uuid { get; set; }
                public string phone_number { get; set; }
                public string name { get; set; }
                public string status_message { get; set; }
                public string contact_name { get; set; }
                public Friends(long type, string uuid, string phone_number, string name, string status_message, string contact_name)
                {
                    this.type = type;
                    this.uuid = uuid;
                    this.phone_number = phone_number;
                    this.name = name;
                    this.status_message = status_message;
                    this.contact_name = contact_name;
                }
            }
            [Serializable]
            public class chatRooms
            {
                public long chat_id { get; set; }
                public string type { get; set; }
                public string member { get; set; }
                public string last_message { get; set; }
                public string last_update { get; set; }
                public chatRooms(long chat_id, string type, string member, string last_message, string last_update)
                {
                    this.chat_id = chat_id;
                    this.type = type;
                    this.member = member;
                    this.last_message = last_message;
                    this.last_update = last_update;
                }
            }
            [Serializable]
            public class chatLogs
            {
                public string name { get; set; }
                public string message { get; set; }
                public string create_at { get; set; }
                public chatLogs( string name, string message, string create_at )
                {
                    this.name = name;
                    this.message = message;
                    this.create_at = create_at;
                }
            }

            public long num;
            public List<Friends> friendsList;
            public Dictionary<long, Friends> friendsDic;
            public List<chatRooms> chatRoomsList;
            public Dictionary<long, List<chatLogs>> chatLogsDic;
            /*
    
public class SMSDataList
{
    public SMSDataList()
    {
        this.data = new List<SMSDataObject>();

        SQLiteConnection myConnection = new SQLiteConnection();
        myConnection.ConnectionString = "Data Source=db\\mmssms.db";
        myConnection.Open();
        SQLiteCommand sqlcmd = new SQLiteCommand(myConnection);
        sqlcmd.CommandText = "select * from sms;";
        SQLiteDataReader reader = sqlcmd.ExecuteReader();
        while (reader.Read())
        {
            this.data.Add(
                new SMSDataObject(
                    "" + reader["type"],
                    "" + reader["body"],
                    "" + reader["address"],
                    "" + reader["date"])
                );
        }

        reader.Close();
        myConnection.Close();
    }

    public List<SMSDataObject> data;
}
 * */
            public KaKaoTalk()
            {
                num = 0;
                friendsList = new List<Friends>();
                friendsDic = new Dictionary<long, Friends>();
                chatRoomsList = new List<chatRooms>();
                chatLogsDic = new Dictionary<long, List<chatLogs>>();
                
                SQLiteConnection kakaoConnection = new SQLiteConnection();
                kakaoConnection.ConnectionString = "Data Source=db\\KakaoTalk.db";
                kakaoConnection.Open();
                SQLiteCommand sqlcmd = new SQLiteCommand(kakaoConnection);
                sqlcmd.CommandText = "select id, type, uuid, phone_number, name, status_message, contact_name from friends;";
                
                SQLiteDataReader reader = sqlcmd.ExecuteReader();
                while (reader.Read())
                {
                    this.friendsDic.Add(
                        (long)reader["id"],
                        new Friends( 
                            (long)reader["type"],
                            "" + reader["uuid"],
                            "" + reader["phone_number"],
                            "" + reader["name"],
                            "" + reader["status_message"],
                            "" + reader["contact_name"]
                            )
                        );
                    this.friendsList.Add(
                        new Friends(
                            (long)reader["type"],
                            "" + reader["uuid"],
                            "" + reader["phone_number"],
                            "" + reader["name"],
                            "" + reader["status_message"],
                            "" + reader["contact_name"]
                            )
                        );
                }
                reader.Close();
                sqlcmd.CommandText = "select id, type, active_member_ids, last_message, last_updated_at from chat_rooms;";
                reader = sqlcmd.ExecuteReader();
                while (reader.Read())
                {
                    string member = "";
                    string member_id = "" + reader["active_member_ids"];
                    member_id = member_id.Replace("[", "");
                    member_id = member_id.Replace("]", "");
                    var memberArray = member_id.Split(',');
                    
                    foreach( string i in memberArray )
                    {
                        long key = Int64.Parse(i);
                        if (friendsDic[key].contact_name == "")
                            member += friendsDic[key].name + ", ";
                        else
                            member += friendsDic[key].contact_name + ", ";
                    }

                    member = member.Remove(member.Length - 2);
                    string time = "" + reader["last_updated_at"];
                    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                    origin = origin.AddSeconds(Double.Parse(time));
                    this.chatRoomsList.Add(
                        new chatRooms(
                            (long)reader["id"],
                            "" + reader["type"],
                            member,
                            "" + reader["last_message"],
                            origin.ToString()
                            )
                        );
                }
                reader.Close();
                sqlcmd.CommandText = "select chat_id, user_id, message, created_at from chat_logs where message not like '{%';";
                reader = sqlcmd.ExecuteReader();
                while (reader.Read())
                {
                    num++;
                    string time = "" + reader["created_at"];
                    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                    origin = origin.AddSeconds(Double.Parse(time));
                    
                    if (!chatLogsDic.ContainsKey((long)reader["chat_id"]))
                    {
                        chatLogsDic.Add((long)reader["chat_id"], new List<chatLogs>());
                            
                        if (!friendsDic.ContainsKey((long)reader["user_id"]))
                        {
                            chatLogsDic[(long)reader["chat_id"]].Add(new chatLogs(
                                "나",
                                "" + reader["message"],
                                origin.ToString()
                                )
                            );
                        }
                        else
                        {
                            if (friendsDic[(long)reader["user_id"]].contact_name == "")
                            {
                                chatLogsDic[(long)reader["chat_id"]].Add(new chatLogs(
                                    friendsDic[(long)reader["user_id"]].name,
                                    "" + reader["message"],
                                    origin.ToString()
                                    )
                                );
                            }
                            else
                            {
                                chatLogsDic[(long)reader["chat_id"]].Add(new chatLogs(
                                    friendsDic[(long)reader["user_id"]].contact_name,
                                    "" + reader["message"],
                                    origin.ToString()
                                    )
                                );
                            }
                        }

                    }
                    else
                    {
                        if (!friendsDic.ContainsKey((long)reader["user_id"]))
                        {
                            chatLogsDic[(long)reader["chat_id"]].Add(new chatLogs(
                                "나",
                                "" + reader["message"],
                                origin.ToString()
                                )
                            );
                        }
                        else
                        {
                            if (friendsDic[(long)reader["user_id"]].contact_name == "")
                            {
                                chatLogsDic[(long)reader["chat_id"]].Add(new chatLogs(
                                    friendsDic[(long)reader["user_id"]].name,
                                    "" + reader["message"],
                                      origin.ToString()
                                    )
                                );
                            }
                            else
                            {

                                chatLogsDic[(long)reader["chat_id"]].Add(new chatLogs(
                                    friendsDic[(long)reader["user_id"]].contact_name,
                                    "" + reader["message"],
                                    origin.ToString()
                                    )
                                );
                            }
                        }
                    }
                }
                reader.Close();
                kakaoConnection.Close();
            }
        }
        [Serializable]
        public class Facebook
        {
            [Serializable]
            public class Notifications
            {
                public string sender_name { get; set; }
                public string updated { get; set; }
                public string title { get; set; }
                public Notifications(string sender_name, string updated, string title)
                {
                    this.sender_name = sender_name;
                    this.updated = updated;
                    this.title = title;
                }
            }
            [Serializable]
            public class Photos
            {
                public string name { get; set; }
                public string url { get; set; }
                public string caption { get; set; }
                public string created { get; set; }
                public Photos(string name, string url, string caption, string created)
                {
                    this.name = name;
                    this.url = url;
                    this.caption = caption;
                    this.created = created;
                }
            }
            [Serializable]
            public class Friends
            {
                public string name { get; set; }
                public string cell { get; set; }
                public string email { get; set; }
                public int birthday_month { get; set; }
                public int birthday_day { get; set; }
                public int birthday_year { get; set; }

                public Friends(string name, string cell, string email, int birthday_month, int birthday_day, int birthday_year)
                {
                    this.name = name;
                    this.cell = cell;
                    this.email = email;
                    this.birthday_day = birthday_day;
                    this.birthday_month = birthday_month;
                    this.birthday_year = birthday_year;
                }
            }
            
            public List<Notifications> notificationsList;
            public List<Photos> photosList;
            public Dictionary<long, Friends> friendsDic;
            public List<Friends> friendsList;
            

            public Facebook()
            {
                notificationsList = new List<Notifications>();
                photosList = new List<Photos>();
                friendsDic = new Dictionary<long, Friends>();
                friendsList = new List<Friends>();

                SQLiteConnection facebookConnection = new SQLiteConnection();
                facebookConnection.ConnectionString = "Data Source=db\\fb.db";
                facebookConnection.Open();
                SQLiteCommand sqlcmd = new SQLiteCommand(facebookConnection);
                sqlcmd.CommandText = "select user_id, first_name, last_name, cell, email, birthday_month, birthday_day, birthday_year from friends_data;";
                SQLiteDataReader reader = sqlcmd.ExecuteReader();
                while (reader.Read())
                {
                    friendsDic.Add(
                        reader.GetInt64(0),
                        new Friends(
                            "" + reader["last_name"] + reader["first_name"],
                            "" + reader["cell"],
                            "" + reader["email"],
                            (int)reader["birthday_month"],
                            (int)reader["birthday_day"],
                            (int)reader["birthday_year"]
                        )
                    );

                    friendsList.Add(
                        new Friends(
                            "" + reader["last_name"] + reader["first_name"],
                            "" + reader["cell"],
                            "" + reader["email"],
                            (int)reader["birthday_month"],
                            (int)reader["birthday_day"],
                            (int)reader["birthday_year"]
                        )
                        );
                }
                reader.Close();


                sqlcmd.CommandText = "select sender_name, updated, title from notifications;";
                reader = sqlcmd.ExecuteReader();
                while (reader.Read())
                {
                    string time = "" + reader["updated"];
                    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                    origin = origin.AddSeconds(Double.Parse(time));
                    notificationsList.Add(
                            new Notifications(
                                "" + reader["sender_name"],
                                origin.ToString(),
                                "" + reader["title"]
                            )
                        );
                }
                reader.Close();

                sqlcmd.CommandText = "select owner, src_big, caption, created from photos;";
                reader = sqlcmd.ExecuteReader();

                while (reader.Read())
                {
                    if (friendsDic.ContainsKey(reader.GetInt64(0)))
                    {
                        string time = "" + reader["created"];
                        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                        origin = origin.AddSeconds(Double.Parse(time));
                        photosList.Add(
                            new Photos(
                                    friendsDic[reader.GetInt64(0)].name,
                                    "" + reader["src_big"],
                                    "" + reader["caption"],
                                    origin.ToString()
                                )
                        );
                    }
                    else
                    {
                        string time = "" + reader["created"];
                        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                        origin = origin.AddSeconds(Double.Parse(time));
                        photosList.Add(
                            new Photos(
                                    "알수없음",
                                    "" + reader["src_big"],
                                    "" + reader["caption"],
                                    origin.ToString()
                                )
                        );
                    }
                    
                    
                }
                reader.Close();
                facebookConnection.Close();
            }

        }
        [Serializable]
        public class MapSearchHistory
        {
            [Serializable]
            public class MapSearch
            {
                public string searchStr { get; set; }
                public string latitude { get; set; }
                public string longitude { get; set; }
                public MapSearch(string searchStr, string latitude, string longitude)
                {
                    this.searchStr = searchStr;
                    this.latitude = latitude;
                    this.longitude = longitude;
                }
            }

            public List<MapSearch> searchList;

            public MapSearchHistory()
            {
                searchList = new List<MapSearch>();

                SQLiteConnection searchConnection = new SQLiteConnection();
                searchConnection.ConnectionString = "Data Source=db\\search_history.db";
                searchConnection.Open();
                SQLiteCommand sqlcmd = new SQLiteCommand(searchConnection);
                sqlcmd.CommandText = "select data1, latitude, longitude from suggestions;";
                SQLiteDataReader reader = sqlcmd.ExecuteReader();
                
                while (reader.Read())
                {
                    searchList.Add(
                            new MapSearch(
                                    "" + reader["data1"],
                                    "" + reader["latitude"],
                                    "" + reader["longitude"]
                                )
                        );
                }
                reader.Close();
                searchConnection.Close();

            }
        }

        [Serializable]
        public class GPSHistory
        {
            [Serializable]
            public class GPS
            {
                public string latitude { get; set; }
                public string longitude { get; set; }
                public string accuracy { get; set; }
                public string time { get; set; }

                public GPS(string latitude, string longitude, string accuracy, string time)
                {
                    this.latitude = latitude;
                    this.longitude = longitude;
                    this.accuracy = accuracy;
                    this.time = time;
                }
            }

            public List<GPS> gpsList;

            public GPSHistory()
            {
                gpsList = new List<GPS>();

                SQLiteConnection searchConnection = new SQLiteConnection();
                searchConnection.ConnectionString = "Data Source=db\\CachedGeoposition.db";
                searchConnection.Open();
                SQLiteCommand sqlcmd = new SQLiteCommand(searchConnection);
                sqlcmd.CommandText = "select * from CachedPosition;";
                SQLiteDataReader reader = sqlcmd.ExecuteReader();

                while (reader.Read())
                {
                    gpsList.Add(
                            new GPS(
                                    "" + reader["latitude"],
                                    "" + reader["longitude"],
                                    "" + reader["accuracy"],
                                    "" + reader["timestamp"]
                                )
                        );
                }
                reader.Close();
                searchConnection.Close();

            }
        }
        [Serializable]
        public class SeoulBus
        {
            [Serializable]
            public class RecentBus
            {
                public string number { get; set; }
                public string time { get; set; }
                public string type { get; set; }
                public string region { get; set; }
                public RecentBus(string number, string time, string type, string region)
                {
                    this.number = number;
                    this.time = time;
                    this.type = type;
                    this.region = region;
                }
            }
            [Serializable]
            public class RecentStation
            {
                
            }

            public List<RecentBus> recentBusList;
            public List<RecentStation> recentStationList;

            public SeoulBus()
            {
                recentBusList = new List<RecentBus>();
                recentStationList = new List<RecentStation>();

                SQLiteConnection seoulBusConnection = new SQLiteConnection();
                seoulBusConnection.ConnectionString = "Data Source=db\\seoulbus.db";
                seoulBusConnection.Open();
                SQLiteCommand sqlcmd = new SQLiteCommand(seoulBusConnection);
                sqlcmd.CommandText = "select region, json, date from recentBus;";
                SQLiteDataReader reader = sqlcmd.ExecuteReader();
                while (reader.Read())
                {
                    int region = Int32.Parse("" + reader["region"]);
                    string strRegion = "";
                    
                    
                    string time = "" + reader["date"];
                    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                    origin = origin.AddSeconds(Double.Parse(time)/1000);
                    
                    string strResponse = "" + reader["json"];
                    JsonTextParser parser = new JsonTextParser();
                    JsonObject obj = parser.Parse(strResponse);
                    JsonObjectCollection col = (JsonObjectCollection)obj;
                    int type = (int)(double)col["routeType"].GetValue();
                    string strType = "";
                    string number = (string)col["routeNumberKor"].GetValue();
                    switch( region )
                    {
                        case 0:
                            strRegion = "서울";
                            break;
                        case 1:
                            strRegion = "경기";
                            break;
                    }
                    switch (type)
                    {
                        case 503:
                            strType = "직행 좌석형 시내버스";
                            break;
                        case 301:
                            strType = "일반형 시내버스";
                            break;
                        case 500:
                            strType = "광역버스";
                            break;
                        case 200:
                            strType = "간선 버스";
                            break;

                    }
                    recentBusList.Add(
                            new RecentBus(
                                    number,
                                    origin.ToString(),
                                    strType,
                                    strRegion
                                )
                        );

                }
                reader.Close();
                sqlcmd.CommandText = "select json from recentStation;";
                reader = sqlcmd.ExecuteReader();
                while (reader.Read())
                {
 
                }
                reader.Close();
                seoulBusConnection.Close();
            }
            
        }
    }
    /*
    
    public class SMSDataList
    {
        public SMSDataList()
        {
            this.data = new List<SMSDataObject>();

            SQLiteConnection myConnection = new SQLiteConnection();
            myConnection.ConnectionString = "Data Source=db\\mmssms.db";
            myConnection.Open();
            SQLiteCommand sqlcmd = new SQLiteCommand(myConnection);
            sqlcmd.CommandText = "select * from sms;";
            SQLiteDataReader reader = sqlcmd.ExecuteReader();
            while (reader.Read())
            {
                this.data.Add(
                    new SMSDataObject(
                        "" + reader["type"],
                        "" + reader["body"],
                        "" + reader["address"],
                        "" + reader["date"])
                    );
            }

            reader.Close();
            myConnection.Close();
        }

        public List<SMSDataObject> data;
    }
     * */
}
